/******************************************
Jonathan Gordon
Blinky port using FreeRTOS to atmega328P
main.c
4/22/2013
******************************************/
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h> 

/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"

void InitPorts(void);
void InitTimers(void);

static void TaskBlinkPortB(void *pvParameters);
static void TaskBlinkPortD(void *pvParameters);

//The idle hook is used to schedule co-routines.
void vApplicationIdleHook(void);

//The tick hook is used by timer routines.
void vApplicationTickHook(void);

int main(void){     
    InitPorts();
    InitTimers();

    xTaskCreate(TaskBlinkPortB, (const signed portCHAR *)"BlinkB", 128, NULL, 2, NULL);

    xTaskCreate(TaskBlinkPortD, (const signed portCHAR *)"BlinkD", 128, NULL, 1, NULL);

    vTaskStartScheduler();

    //and finally, to eliminate compiler warnings....
    return(0);
}



static void TaskBlinkPortB(void *pvParameters){
	(void) pvParameters;
	char bits=0;
	const portTickType xDelay = 2000 / portTICK_RATE_MS;
	while(1){
		PORTB=bits;
		bits=~bits;
		vTaskDelay(xDelay);
	}
}

static void TaskBlinkPortD(void *pvParameters){
    (void) pvParameters;
    char bits=0;
    const portTickType xDelay = 100 / portTICK_RATE_MS;
    while(1){
        PORTD=bits;
        bits=~bits;
        vTaskDelay(xDelay);
    }
}



//--------Init Ports --------------------
// Input/Output Ports initialization
// Setting DDRx to 1 sets as output
void InitPorts(void){
	// Port B initialization
    // 0 - output - no connection - but internally, spi SS should be output
    // 1 - output - spi clock
    // 2 - output - spi data - MOSI
    // 3 - input  - spi data - MISO
    // 4 - output - chip enable - spi rtc
    // 5 - output - chip enable - spi flash
    // 6 - output - lcd backlight power enable
    // 7 - output - ibutton led power enable
    // 0b1111 0111 = 0xf7
	PORTB=0x00;
	DDRB=0x20;

	// Port C initialization
    // 0 - output - lcd data
    // 1 - output - lcd data
    // 2 - output - lcd data
    // 3 - output - lcd data
    // 4 - output - LED Green
    // 5 - output - lcd data
    // 6 - output - lcd data
    // 7 - output - lcd data
	PORTC=0x00;
	DDRC=0x10;

	// Port D initialization
    // 0 - input  - big red button
    // 1 - input  - LED Red
    // 2 - input  - uart1 RX
    // 3 - output - uart1 TX
    // 4 - input  - nav2
    // 5 - input  - nav3
    // 6 - input  - nav4
    // 7 - input  - nav5
    // 0b00000100 = 0x04
   	PORTD=0x00;
	DDRD=0x0f;
}

//--------Init Timers --------------------
void InitTimers(void){
	// Timer/Counter 0 initialization
	// Clock source: System Clock
	// Clock value: Timer 0 Stopped
	// Mode: Normal top=FFh
	// OC0 output: Disconnected
	// ASSR=0x00;
	// TCCR0=0x00;
	// TCNT0=0x00;
	// OCR0=0x00;

	// Timer/Counter 1 initialization
	// Mode: Normal top=FFFFh


	// OC1A output: Discon.
	// OC1B output: Discon.
	// OC1C output: Discon.
	TCCR1A=0x00;

	// Clock source: none               = 00
	// Clock source: system clock       = 01
	// Clock source: system clock/64    = 03
	// Clock source: system clock/256   = 04
	// Clock source: system clock/1024  = 05
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	TCCR1B=0x00;

    // set the inital counts to zero
	TCNT1H=0x00;
	TCNT1L=0x00;

    // amount to compare the timer to
	OCR1AH=0x00;
	OCR1AL=0x00;

	OCR1BH=0x00;
	OCR1BL=0x00;


	// Timer/Counter 2 initialization
	// Clock source: T2 pin Rising Edge
	// Mode: Normal top=FFh
	// OC2 output: Disconnected
	TCCR2B=0x07;
	TCNT2=0x00;
	OCR2A=0x00;

    } 


/*-----------------------------------------------------------*/

void vApplicationIdleHook( void )
{
    ;
}

/*-----------------------------------------------------------*/

void vApplicationTickHook( void )
{
    ;
}

